﻿
Date.getDaysInMonth = function (a) {
    var b = new Date(a);
    b.setMonth(b.getMonth() + 1);
    b.setDate(0);
    return b.getDate();
}

Date.dateEqual = function (a, b) {
    if (a.getFullYear() == b.getFullYear() && a.getMonth() == b.getMonth() && a.getDate() == b.getDate()) {
        return true;
    }
    return false;
}

Lscheduler.getTitleText = function (date) {
    return Lscheduler.supplyString((date.getMonth() + 1)) + "/" + Lscheduler.supplyString(date.getDate()) + " (" + Lscheduler.week[date.getDay()] + ")";
}

Lscheduler.getTitleText_zhcn = function (date) {
    return Lscheduler.supplyString((date.getMonth() + 1)) + "月" + Lscheduler.supplyString(date.getDate()) + "日（" + Lscheduler.week[date.getDay()] + "）";
}

Lscheduler.getDate = function (date) {
    return date.getFullYear().toString() + "-" + Lscheduler.supplyString((date.getMonth() + 1)) + "-" + Lscheduler.supplyString(date.getDate());
}

//格式转化 例2013-08-30 12:34:22
Lscheduler.parseDate = function (date) {
    var t = date.split(" ");
    var t1 = t[0].split("-");
    if (t[1]) {
        t = t[1].split(":");
    }
    else {
        t = 1;
    }
    return new Date(t1[0], parseInt(t1[1]) - 1, t1[2], t[0] || 0, t[1] || 0, t[2] || 0);
}

Lscheduler.miniWeek = ["一", "二", "三", "四", "五", "六", "日"];
Lscheduler.week = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];

Lscheduler.supplyString = function (content, digit) {
    if (!digit) {
        digit = 2;
    }
    content = content.toString();
    while (content.length < digit) {
        content = "0" + content;
    }
    return content;
}

Lscheduler.getText = function (element) {
    try {
        return element.innerText;
    }
    catch (err) { return element.textContent; }
}

Lscheduler.setText = function (element, content) {
    element.innerHTML = "";
    element.appendChild(document.createTextNode(content));
}

Lscheduler.createButton = function (text) {
    var btn = document.createElement("input");
    btn.type = "button";
    btn.className = "ls_btn";
    btn.value = text;
    return btn;
}

Lscheduler.createAnchor = function (text) {
    var btn = document.createElement("a");
    btn.appendChild(document.createTextNode(text));
    btn.href = "javascript:void(0)";
    btn.className = "ls_btn_a";
    btn.value = text;
    return btn;
}

Lscheduler.stopBubble = function (e) {
    if (!e && window.event) {
        e = window.event;
    }
    if (!e) {
        return;
    }
    if (document.all) {
        e.cancelBubble = true;
    }
    else {
        e.stopPropagation();
    }
}

Lscheduler.getPosition = function (element) {
    var pos = { x: element.offsetTop, y: element.offsetLeft };
    if (element.offsetParent != null) {
        var temp = Lscheduler.getPosition(element.offsetParent);
        pos.x += temp.x;
        pos.y += temp.y;
    }
    return pos;
}

//ajax请求 url:请求地址 type:post|get async:true|false success:func error:func data:object 
Lscheduler.ajax = function (cfg) {
    if (!cfg.url) {
        return;
    }
    if (!cfg.type) {
        cfg.type = "get";
    }
    if (cfg.async != false && cfg.async != true) {
        cfg.async = true;
    }
    if (!Lscheduler.xmlHttp) {
        if (window.ActiveXObject) {
            Lscheduler.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        else if (window.XMLHttpRequest) {
            Lscheduler.xmlHttp = new XMLHttpRequest();
        }
        else {
            alert("您的浏览器不支持异步数据访问操作");
            return;
        }
    }
    Lscheduler.xmlHttp.onreadystatechange = function () {
        if (Lscheduler.xmlHttp.readyState == 4) {
            Lscheduler.hideMask();
            if (Lscheduler.xmlHttp.status == 200) {
                cfg.success(Lscheduler.xmlHttp.responseText);
            }
            else {
                if (cfg.error) {
                    cfg.error(Lscheduler.xmlHttp.responseText);
                }
            }
            if (cfg.complete) {
                cfg.complete();
            }
        }
    }
    Lscheduler.xmlHttp.open(cfg.type, cfg.url, cfg.async);
    if (cfg.data) {
        if (typeof (cfg.data) == "object") {
            var temp = "", flag = 0;
            for (var i in cfg.data) {
                if (flag == 1) {
                    temp += "&";
                }
                else {
                    flag = 1;
                }
                temp += i + "=" + cfg.data[i];
            }
            cfg.data = temp;
        }
        //Lscheduler.xmlHttp.setRequestHeader("Accept", "*/*");
        Lscheduler.xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
        //Lscheduler.xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        //Lscheduler.xmlHttp.setRequestHeader("X-Requested-Width", "XMLHttpRequest");
    }
    Lscheduler.loadMask();
    Lscheduler.xmlHttp.send(cfg.data);
}

Lscheduler.parseJson = function (content) {
    return (new Function("return " + content))();
}

Lscheduler.trim = function (content) {
    return Lscheduler.rTrim(Lscheduler.lTrim(content));
}

Lscheduler.lTrim = function (str) {
    var i;
    for (i = 0; i < str.length; i++) {
        if (str.charAt(i) != " " && str.charAt(i) != " ") break;
    }
    str = str.substring(i, str.length);
    return str;
}

Lscheduler.rTrim = function (str) {
    var i;
    for (i = str.length - 1; i >= 0; i--) {
        if (str.charAt(i) != " " && str.charAt(i) != " ") break;
    }
    str = str.substring(0, i + 1);
    return str;
}

//添加事件 
Lscheduler.attachEvent = function (src, eventName, func) {
    if (src.attachEvent) {
        src.attachEvent("on" + eventName, func);
    }
    else {
        src.addEventListener(eventName, func, false);
    }
}

//移除事件
Lscheduler.removeEvent = function (src, eventName, func) {
    if (src.detachEvent) {
        src.detachEvent("on" + eventName, func);
    }
    else {
        src.removeEventListener(eventName, func, false);
    }
}


Lscheduler.moveRight = function (node, startLeft, desLeft, func) {
    node.style.left = startLeft + "px";
    var v = setInterval(function () {
        var left = node.offsetLeft - 50;
        if (left <= (desLeft + 500)) {
            left = left - 50;
        }
        if (left <= desLeft) {
            node.style.left = desLeft + "px";
            clearInterval(v);
            if (func) {
                func();
            }
        }
        else {
            node.style.left = left + "px";
        }
    }, 15);
}

Lscheduler.registerButtonEffect = function (button) {
    Lscheduler.attachEvent(button, "mouseover", function () {
        button.style.boxShadow = "0 0 5px #848484";
    });
    Lscheduler.attachEvent(button, "mouseout", function () {
        button.style.boxShadow = "";
    });
    Lscheduler.attachEvent(button, "mousedown", function () {
        button.style.boxShadow = "inset 0 0 5px #848484";
    });
    Lscheduler.attachEvent(button, "mouseup", function () {
        button.style.borderColor = "";
    });
}

Lscheduler.getImitateButton = function (text) {
    var div = document.createElement("div");
    div.className = "ls_btn_imi";
    if (text) {
        div.appendChild(document.createTextNode(text));
    }
    Lscheduler.registerButtonEffect(div);
    return div;
}

Lscheduler.getTextbox = function () {
    var txt = document.createElement("input");
    txt.type = "text";
    txt.className = "ls_txt1";
    return txt;
}

Lscheduler.colorSet = [{ name: "宝石蓝", value: "#5484ed" }, { name: "蓝色", value: "#a4bdfc" }, { name: "青绿色", value: "#46d6db" }, { name: "绿色", value: "#7ae7bf" }, { name: "清新绿", value: "#51b749" }, { name: "黄色", value: "#fbd75b" }, { name: "橙色", value: "#ffb878" }, { name: "红色", value: "#ff887c" }, { name: "奢华红", value: "#dc2127" }, { name: "紫色", value: "#dbadff" }, { name: "灰色", value: "#e1e1e1" }];

Lscheduler.loadMask = function (notShowLoading) {
    var div = document.getElementById("div_mask");
    if (!div) {
        div = document.createElement("div");
        div.style.height = "100%";
        div.id = "div_mask";
        div.style.width = "100%";
        div.style.position = "absolute";
        div.style.zIndex = "8899";
        div.style.backgroundColor = "#F3F3F3";
        var v1 = document.createElement("div");
        v1.className = "ls_loading";
        document.body.appendChild(div);
        div.appendChild(v1);
    }
    else {
        div.style.display = "";
    }
    if (notShowLoading) {
        div.childNodes[0].style.display = "none";
    }
    else {
        div.childNodes[0].style.display = "";
    }
    div.style.opacity = "0";
    div.setAttribute("l", "1");
    var v = setInterval(function () {
        if (!div.getAttribute("l")) {
            clearInterval(v);
            return;
        }
        var op = parseFloat(div.style.opacity) + 0.2;
        if (op >= 0.8) {
            div.style.opacity = "0.8";
            clearInterval(v);
        }
        else {
            div.style.opacity = op.toString();
        }
    }, 20);
}

Lscheduler.hideMask = function () {
    var div = document.getElementById("div_mask");
    if (!div) {
        return;
    }
    div.removeAttribute("l");
    var v = setInterval(function () {
        if (div.getAttribute("l")) {
            clearInterval(v);
            return;
        }
        var op = parseFloat(div.style.opacity) - 0.2;
        if (op <= 0) {
            div.style.opacity = "0";
            clearInterval(v);
            div.style.display = "none";
        }
        else {
            div.style.opacity = op.toString();
        }
    }, 20);
}

Lscheduler.getActivityContainer = function (backgroundColor) {
    var div = document.createElement("div");
    div.style.backgroundColor = backgroundColor;
    div.style.zIndex = 8;
    div.style.display = "inline-block";
    div.style.position = "absolute";
    div.style.border = "1px solid #929292";
    div.onclick = function (e) {
        Lscheduler.stopBubble(e);
    }
    return div;
}

Lscheduler.getDaysInMonth = function (year, month) {
    var b = new Date(year, month, 1);
    b.setMonth(b.getMonth() + 1);
    b.setDate(0);
    return b.getDate();
}

Lscheduler.dateEqual = function (a, b) {
    if (a.getFullYear() == b.getFullYear() && a.getMonth() == b.getMonth() && a.getDate() == b.getDate()) {
        return true;
    }
    return false;
}

Lscheduler.getFullMonthDays = function (year, month) {
    var startDate = new Date(year, month, 1, 0, 0, 0);
    var week = startDate.getDay();
    if (week != 1) {
        if (week == 0) {
            week = 7;
        }
        //若不为周一，则开始日期为周一
        startDate = new Date(startDate.valueOf() - (week - 1) * 24 * 60 * 60 * 1000);
    }
    var endDate = new Date(year, month, Lscheduler.getDaysInMonth(year, month), 23, 59, 59);
    week = endDate.getDay();
    if (week != 0) {
        endDate = new Date(endDate.valueOf() + (7 - week) * 24 * 60 * 60 * 1000);
    }
    var dates = [];
    do {
        dates.push(startDate);
        if (Lscheduler.dateEqual(endDate, startDate)) {
            break;
        }
        startDate = new Date(startDate.valueOf() + 24 * 60 * 60 * 1000);
    }
    while (1 == 1);
    if (dates.length == 35) {
        for (var i = 0; i < 7; i++) {
            startDate = new Date(startDate.valueOf() + 24 * 60 * 60 * 1000);
            dates.push(startDate);
        }
    }
    return dates;
}

Lscheduler.isDateFormat = function (content) {
    var datereg = /((^((1[8-9]\d{2})|([2-9]\d{3}))(-)(10|12|0?[13578])(-)(3[01]|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))(-)(11|0?[469])(-)(30|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))(-)(0?2)(-)(2[0-8]|1[0-9]|0?[1-9])$)|(^([2468][048]00)(-)(0?2)(-)(29)$)|(^([3579][26]00)(-)(0?2)(-)(29)$)|(^([1][89][0][48])(-)(0?2)(-)(29)$)|(^([2-9][0-9][0][48])(-)(0?2)(-)(29)$)|(^([1][89][2468][048])(-)(0?2)(-)(29)$)|(^([2-9][0-9][2468][048])(-)(0?2)(-)(29)$)|(^([1][89][13579][26])(-)(0?2)(-)(29)$)|(^([2-9][0-9][13579][26])(-)(0?2)(-)(29)$))/;
    if (!datereg.test(content)) {
        return false;
    }
    else {
        return true;
    }
}


Lscheduler.createElement = function (tagName, attributes, styles) {
    var v = document.createElement(tagName);
    if (attributes) {
        for (var a in attributes) {
            if (a == "className") {
                v.setAttribute("class", attributes[a]);
            }
            else {
                v.setAttribute(a, attributes[a]);
            }
        }
    }
    if (styles) {
        for (var s in styles) {
            v.style[s] = styles[s];
        }
    }
    return v;
}

Lscheduler.setCursorPosition = function (textbox, position) {
    if (document.all) {
        var r = textbox.createTextRange();
        r.collapse(true);
        r.moveStart("character", position);
        r.select();
    }
    else {
        textbox.setSelectionRange(position, position);
    }
}

Lscheduler.getCursorPosition = function (textbox, isContaineEnd) {
    if (document.all) {
        var c = document.activeElement, temp = 0;
        textbox.focus();
        var rang = document.selection.createRange();
        if (isContaineEnd) {
            temp = rang.text.length;
        }
        rang.setEndPoint("StartToStart", textbox.createTextRange());
        c.focus();
        if (isContaineEnd) {
            return {
                start: rang.text.length - temp
                , end: rang.text.length
            };
        }
        else {
            return rang.text.length;
        }
    }
    else {
        if (isContaineEnd) {
            return { start: textbox.selectionStart, end: textbox.selectionEnd };
        }
        else {
            return textbox.selectionStart;
        }
    }
}

Lscheduler.selectText = function (textbox, start, len) {
    if (document.all) {
        var r = textbox.createTextRange();
        r.moveEnd('character', len - (textbox.value.length - start));
        r.moveStart('character', start);
        r.select();
    }
    else {
        textbox.setSelectionRange(start, start + len);
    }
}

Lscheduler.insertCharacter = function (content, character, position) {
    return content.substr(0, position) + character + content.substring(position, content.length);
}

Lscheduler.removeCharacter = function (content, position, offset, direction) {
    if (!offset) {
        offset = 1;
    }
    if (direction == "delete") {
        return content.substr(0, position) + content.substring(position + offset, content.length);
    }
    else {
        return content.substr(0, position - offset) + content.substring(position, content.length);
    }
}

Lscheduler.getChangedContent = function (textbox, key) {
    var temp = Lscheduler.getCursorPosition(textbox, true);
    if (temp.end == 0) {
        return Lscheduler.insertCharacter(textbox.value, key, temp.start);
    }
    else {
        return textbox.value.substr(0, temp.start) + key + textbox.value.substring(temp.end, textbox.value.length);
    }
}

Lscheduler.registerTimeLimit = function (textbox) {
    textbox.onkeydown = function (event) {
        var keyCode = event.keyCode || event.witch || event.charCode;
        if (keyCode >= 48 && keyCode <= 57) {
            if (Lscheduler.validateContent(Lscheduler.getChangedContent(this, String.fromCharCode(keyCode)), textbox) == false) {
                return false;
            }
        }
        else if (event.shiftKey && keyCode == 186) {
            var temp = Lscheduler.getCursorPosition(textbox, true);
            if (temp.end == 0) {
                if (((this.value.indexOf(":") == -1 && !/^([0-1][0-9]|[2][0-3]|[0-9])$/.test(this.value)) || this.value.indexOf(":") > -1)) {
                    return Lscheduler.insertCharacter(this.value, key, temp.start);
                }
                else if (Lscheduler.validateContent(Lscheduler.getChangedContent(this, ":"), textbox) == false) {
                    return false;
                }
            }
        }
        else if (keyCode == 8) {
            var pos = Lscheduler.getCursorPosition(this, true);
            var c = this.value.substr(pos.start, pos.end - pos.start);
            if (pos.end == pos.start) {
                var c = this.value.substr(pos.start - 1, 1);
                if (c == ":") {
                    Lscheduler.setCursorPosition(this, pos.start - 1);
                    return false;
                }
            }
            else if (this.value.substr(pos.start, pos.end - pos.start).indexOf(":") > -1) {
                this.value = this.value.substring(0, pos.start) + ":" + this.value.substring(pos.end, this.value.length);
                Lscheduler.setCursorPosition(this, pos.start);
                return false;
            }
        }
        else if (keyCode == 46) {
            var pos = Lscheduler.getCursorPosition(this, true);
            if (pos.end == pos.start) {
                var c = this.value.substr(pos.start, 1);
                if (c == ":") {
                    Lscheduler.setCursorPosition(this, pos.start + 1);
                    return false;
                }
            }
            else if (this.value.substr(pos.start, pos.end - pos.start).indexOf(":") > -1) {
                this.value = this.value.substring(0, pos.start) + ":" + this.value.substring(pos.end, this.value.length);
                Lscheduler.setCursorPosition(this, pos.start);
                return false;
            }
        }
        else if (!((keyCode >= 37 && keyCode <= 40) || keyCode == 46 || keyCode == 9 || keyCode == 16 || keyCode == 116)) {
            return false;
        }
    }
    textbox.onblur = function () {
        var content = this.value;
        if (content.length == 0 || content == ":") {
            this.value = "00:00";
        }
        else if (content.length == 1) {
            if (/^\d$/.test(content)) {
                this.value = "0" + content + ":00";
            }
        }
        else if (content.length == 2) {
            if (/^[0-1][0-9]|[2][0-3]$/.test(content)) {
                this.value = content + ":00";
            }
            else if (/^[0-9]:$/.test(content)) {
                this.value = "0" + content + "00";
            }
            else if (/^:[0-9]$/.test(content)) {
                this.value = "00:0" + content.substr(1, 1);
            }
            else if (/^2[4-9]|[3-5][0-9]$/.test(content)) {
                this.value = "00:" + content;
            }
        }
        else if (content.length == 3) {
            if (/^[0-1][0-9]|[2][0-3]:$/.test(content)) {
                this.value = content + "00";
            }
            else if (/^[0-9]:[0-9]$/.test(content)) {
                this.value = "0" + content.substr(0, 2) + "0" + content.substr(2, 1);
            }
            else if (/^:[0-5][0-9]$/.test(content)) {
                this.value = "00" + content;
            }
        }
        else if (content.length == 4) {
            if (/^([0-1][0-9]|[2][0-3]):[0-9]$/.test(content)) {
                this.value = content.substr(0, 3) + "0" + content.substr(3, 1);
            }
            else if (/^[0-9]:[0-5][0-9]$/.test(content)) {
                this.value = "0" + content;
            }
        }
    }
}

Lscheduler.validateContent = function (content, textbox) {
    if (content.length == "1") {
    }
    else if (content.length == 2) {
        if (/^([0-1][0-9]|[2][0-3]:?|[0-9]:|:[0-5])$/.test(content)) {
            if (content.indexOf(":") == -1) {
                textbox.value = content + ":";
                Lscheduler.selectText(textbox, content.length + 1, 0);
                return false;
            }
        }
        else if (textbox.value.indexOf(":") == -1 && /^[0-5][0-9]$/.test(content)) {
            textbox.value = ":" + content;
            Lscheduler.selectText(textbox, 0, 0);
            return false;
        }
        else {
            return false;
        }
    }
    else if (content.length == 3) {
        if (/^(([0-1][0-9]|[2][0-3]):)$/.test(content)) {
            textbox.value = content;
            Lscheduler.setCursorPosition(textbox, content.length);
            return false;
        }
        else if (/^:[0-5][0-9]/.test(content)) {
            textbox.value = content;
            Lscheduler.setCursorPosition(textbox, content.length);
            return false;
        }
        else if (!/^([0-9]:[0-9])$/.test(content)) {
            return false;
        }
    }
    else if (content.length == 4) {
        if (!/^((([0-1][0-9]|[2][0-3]):[0-9])|([0-9]:[0-5][0-9]))$/.test(content)) {
            return false;
        }
    }
    else if (content.length == 5) {
        if (!/^([0-1][0-9]|[2][0-3]):[0-5][0-9]$/.test(content)) {
            return false;
        }
    }
    else {
        return false;
    }
    return true;
}


Lscheduler.Message = {};

Lscheduler.Message.alert = function (message) {
    Lscheduler.loadMask(true);
    var c = document.getElementById("ls_messagebox");
    if (!c) {
        c = Lscheduler.createElement("div", { id: "ls_messagebox" }, { height: "150px", width: "280px", zIndex: 9900, position: "absolute" });
        var cx = Lscheduler.createElement("div", null, { backgroundColor: "#D7D7D7", opacity: 0.6, width: "280px", height: "150px", zIndex: 9901 });
        c.appendChild(cx);
        var ca = Lscheduler.createElement("div", null, { margin: "-140px 0px 0px 10px", zIndex: 9902, position: "absolute" });
        c.appendChild(ca);
        var c1 = Lscheduler.createElement("div", null, { borderBottom: "1px solid #c6c6c6", width: "260px", height: "26px", backgroundColor: "#c6c6c6", lineHeight: "28px" });
        ca.appendChild(c1);
        var client = { x: 0, y: 0, x1: 0, y1: 0 }
        c1.onmousedown = function (event) {
            this.style.cursor = "move";
            Lscheduler.attachEvent(document.body, "mousemove", onmousemove);
            Lscheduler.attachEvent(document.body, "mouseup", onmouseup);
            client.x = event.clientX;
            client.y = event.clientY;
            client.x1 = c.offsetLeft;
            client.y1 = c.offsetTop;
        }
        var onmousemove = function (event) {
            var temp = event.clientX - client.x + client.x1;
            if (temp < 0) {
                temp = 0;
            }
            else if (temp > (Lscheduler.screenWidth - c.offsetWidth)) {
                temp = Lscheduler.screenWidth - c.offsetWidth;
            }
            c.style.left = temp + "px";
            var temp = event.clientY - client.y + client.y1;
            if (temp < 0) {
                temp = 0;
            }
            else if (temp > (Lscheduler.screenHeight - c.offsetHeight)) {
                temp = Lscheduler.screenHeight - c.offsetHeight;
            }
            c.style.top = temp + "px";
        }
        var onmouseup = function (event) {
            Lscheduler.removeEvent(document.body, "mousemove", onmousemove);
            Lscheduler.removeEvent(document.body, "mouseup", onmouseup);
            c.style.cursor = "default";
        }
        var c11 = Lscheduler.createElement("div", null, { float: "left", textIndent: "10px", color: "#493232" });
        c11.appendChild(document.createTextNode("提示"));
        c1.appendChild(c11);
        c11 = Lscheduler.createElement("div", { className: "ls_icon_close2" }, { float: "right", margin: " 8px 10px 0px 0px", cursor: "pointer" });
        var close = function () {
            c.style.display = "none";
            Lscheduler.hideMask();
        }
        c11.onclick = close;
        c1.appendChild(c11);
        c1 = Lscheduler.createElement("div", null, { border: "1px solid #c6c6c6", width: "258px", height: "100px", backgroundColor: "#FFFFFF" });
        ca.appendChild(c1);
        c11 = Lscheduler.createElement("div", { id: "ls_message_content" }, { width: "100%", textAlign: "center", height: "50px", lineHeight: "50px" });
        //c11.appendChild(document.createTextNode("提示信息"));
        c1.appendChild(c11);
        c11 = Lscheduler.createElement("div", null, { margin: "auto", width: "90px" });
        var btn = Lscheduler.getImitateButton("确定");
        btn.onclick = close;
        c11.appendChild(btn);
        c1.appendChild(c11);
        document.body.appendChild(c);
    }
    document.getElementById("ls_message_content").innerHTML = "";
    document.getElementById("ls_message_content").appendChild(document.createTextNode(message));
    c.style.left = ((Lscheduler.screenWidth - c.offsetWidth) / 2) + "px";
    c.style.top = ((Lscheduler.screenHeight - c.offsetHeight) / 2) + "px";

}